#!/usr/bin/env python

################################################################################
#
# Pop Zombie Game
#
# Font Texture creator
# to run this tool you will need python ;) and Python Imaging Library
# you can find it at: http://www.pythonware.com/library/index.htm
#
################################################################################

import sys
import Image,ImageFont, ImageDraw

class DimensionError (Exception):
	pass

class FontTextureCreator:
	def __init__(self, font, size, txt, dimension):
		self.font = ImageFont.truetype(font, size)
		self.txt = open(txt,"w")
		
		self.image = Image.new("RGBA", (dimension,dimension))
		self.draw = ImageDraw.Draw(self.image)
		
		self.x = 0
		self.y = 0
		self.texW = dimension
		self.texH = dimension
		self.txt.write("%d\n"%dimension)
		
	
	def printChar(self,char):
		ch = chr(char)
		(w,h) = self.font.getsize(ch)
		
		if self.x + w >= self.texW:
			self.x = 0
			self.y += h
			if self.y >= self.texH:
				raise DimensionError()
		
		self.txt.write("%s %d %d %d %d\n" % (ch, self.x, self.y, w, h))
		self.draw.text((self.x, self.y), ch, font=self.font)
		self.x += w
		
	def process(self):
		for i in xrange(32,127):
			self.printChar(i)
			
	def save(self, imgName):
		self.image.save(imgName)
		
def main():
	dim = 256
	while True:
		try:
			creator = FontTextureCreator("font.ttf",30,"font.txt", dim)
			creator.process()
			creator.save("font.png")
			break
		except DimensionError:
			dim *= 2

	
if __name__ == "__main__":
	main()

